package com.guigu.base.controller;


import com.guigu.base.entity.Truck;
import com.guigu.base.entity.TruckLicense;
import com.guigu.base.entity.dto.TruckLicenseDto;
import com.guigu.base.service.TruckLicenseService;
import com.guigu.base.service.TruckService;
import com.guigu.tms.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 车辆行驶证表 前端控制器
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-08
 */
@RestController
@RequestMapping("/goods/pd-truck-license")
@Api(tags = "车辆行驶证表管理")
public class TruckLicenseController {
    @Autowired
    private TruckLicenseService truckLicenseService;
    @Autowired
    private TruckService truckService;

    /**
     * 保存车辆行驶证信息
     *
     * @param dto 车辆行驶证信息
     * @return 车辆行驶证信息
     */
    @PostMapping("")
    @ApiOperation("保存车辆行驶证信息")
    public R<TruckLicenseDto> saveTruckLicense(@RequestBody TruckLicenseDto dto) {
        TruckLicense pdTruckLicense = new TruckLicense();
        BeanUtils.copyProperties(dto, pdTruckLicense);
        pdTruckLicense = truckLicenseService.saveTruckLicense(pdTruckLicense);
        if (dto.getId() == null) {
            Truck truck = new Truck();
            truck.setId(dto.getId());
            truck.setTruckLicenseId(pdTruckLicense.getId());
            truckService.saveTruck(truck);
        }
        BeanUtils.copyProperties(pdTruckLicense, dto);
        return new R<>("2000",dto);
    }

    /**
     * 根据id获取车辆行驶证详情
     *
     * @param id 车辆行驶证id
     * @return 车辆行驶证信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取车辆行驶证详情")
    public R<TruckLicenseDto> fineById(@PathVariable(name = "id") String id) {
        TruckLicense pdTruckLicense = truckLicenseService.getById(id);
        TruckLicenseDto dto = new TruckLicenseDto();
        BeanUtils.copyProperties(pdTruckLicense, dto);
        return new R<>("2000",dto);
    }
}

